<?php
    class System extends Admin_Controller {

	
	
        public function index()
        {
        }
		
		//重置密码
		public function resetpwd()
		{
		//$this->output->enable_profiler(TRUE);
		   $this->load->helper('common_function');
			$id = $this->input->get('id',true);
			  $data = array(
                    'pwd' => md5("111111")
                );
				    $this->load->model('admin/System_model');
                $this-> System_model->pwd_update($id,$data);
               ShowMsg('密码重置成功,新密码为 111111 ', site_url("admin/system/account_index"),0,3000);
               exit;
		}

		//修改密码
        public function pwd()
        {
            $action = $this->input->post('action',true);
            if($action=='update')
            {
                $this->load->helper('common_function');
                $this->load->model('admin/System_model');
                $pwd = $this->input->post('pwd',true);
                $lpwd = $this->input->post('lpwd',true);
                if(empty($pwd))
                {
                    ShowMsg('输入密码!', site_url("admin/system/pwd"),0,1000);
                    exit; 
                }
                if($pwd!=$lpwd)
                {
                    ShowMsg('两次密码输入不一致!', site_url("admin/system/pwd"),0,1000);
                    exit;   
                }


                //获取登录用户ID
                $id= $this->_admin->id;
                //需要更新的数据
                $data = array(
                    'pwd' => md5($pwd)
                );
                $this-> System_model->pwd_update($id,$data);
                ShowMsg('密码修改成功!', site_url("admin/system/pwd"),0,1000);
                exit;

            }
            else
            {
                $this->load->view('admin/system_pwd');
            }
        }

        public function info()
        {
            //获取系统配置信息
            $dopost = $this->input->post('dopost',true);
            $this->load->model('admin/System_model');
            if($dopost=='save')
            {
                $this->load->helper('common_function');
                foreach($_POST as $k=>$v)
                {
                
                   // echo '<br/>'.$k.'=>'.$v;
                    /*
                    if(preg_match("#^edit___#", $k))
                    {
                        $v = cn_substrR(${$k}, 1024);
                    }
                    else
                    {
                        continue;
                    }
                    */
                    $k = preg_replace("#^edit___#", "", $k);
                    $data=array(
                        'value'=>$v
                    );
                    $this-> System_model->info_update($k,$data);
                    
                }
                 
                ShowMsg("成功更改站点配置！", site_url("admin/system/info"));
                exit();
                
            }


            $result=$this-> System_model->info_all();
            $data['model']=$result;
            $this->load->view('admin/system_info',$data);
        }

		
		
		public function account_index()
	{
		
		//检查是否有超级管理员权限
		$this->_check_admin();
		
		
		$whereSql = " WHERE 1=1 ";
		
		$query = "SELECT * FROM `crazy_admin` $whereSql ";
		
		$list = $this->db->query($query);
		
		// 从数据库中获取列表总数
		$num = $list->num_rows();
		
		// 查询条件
		$search_str = "";
		
		// 分类数据初始化
		$params = array( 
			'total' => $num, 
			'listRows' => '10', 
			'pa' => $search_str 
		);
		
		// 分页类初始化
		$this->load->library('Page', $params);
		
		// 分页SQL语句
		$search_query = $query . "  limit {$this->page->limit} ";
		
		// 获取分页结果
		$reslut_list = $this->db->query($search_query);
		
		$data['list'] = $reslut_list;
		
		$data['page'] = $this->page->fpage();
		
		$this->load->view('admin/account_index', $data);
	}
	
	public function account_add()
	{
		$this->load->view('admin/account_add');
	}
	public function account_modify()
	{
		//检查是否有超级管理员权限
		$this->_check_admin();
		
		
		$id = $this->input->get('id', true);
		$this->load->model('admin/User_model');
		$data['model'] = $this->User_model->get_user_by_id_one($id);
		$this->load->view('admin/account_modify', $data);
	}
	public function account_save()
	{
		//检查是否有超级管理员权限
		$this->_check_admin();
		
		
		$this->load->helper('common_function');
		$action = $this->input->post('action', true);
		$this->load->model('admin/User_model');
		if ($action == "add")
		{
			$usertype = $this->input->post('usertype', true);
			$userid = $this->input->post('userid', true);
			$pwd = $this->input->post('pwd', true);
			$tel = $this->input->post('tel', true);
			$uname = $this->input->post('uname', true);
			$email = $this->input->post('email', true);
			$typeid = $this->input->post('typeid', true);
			$logintime = date("Y-m-d H:i:s");
			$loginip = '';
			$tname=$uname;
			
			
			
			$data = array( 
				'usertype' => $usertype, 
				'userid' => $userid, 
				'pwd' => md5($pwd), 
				'tel' => $tel, 
				'uname' => $uname, 
				'email' => $email, 
				'typeid' => $typeid, 
				'logintime' => $logintime, 
				'loginip' => $loginip,
				'tname'=>$tname
			);
			$this->User_model->user_save_add($data);
			ShowMsg('添加成功!', site_url("admin/system/account_index"), 0, 1000);
			exit();
		}
		if ($action == "update")
		{
			$id = $this->input->post('id', true);
			$usertype = $this->input->post('usertype', true);
		 
		 
			$tel = $this->input->post('tel', true);
			$uname = $this->input->post('uname', true);
			$email = $this->input->post('email', true);
			$typeid = $this->input->post('typeid', true);
			$tname=$uname;
			
		 
			
			$data = array( 
				'usertype' => $usertype, 
				'tel' => $tel, 
				'uname' => $uname, 
				'email' => $email, 
				'typeid' => $typeid, 
				'tname' => $tname
			);
			$this->User_model->user_save_modify($id, $data);
			ShowMsg('编辑成功!', site_url("admin/system/account_index"), 0, 1000);
			exit();
		}
	}
	
	//删除
	public function account_del()
	{
		//检查是否有超级管理员权限
		$this->_check_admin();
		
	
		$this->load->helper('common_function');
		$aids = $this->input->get('aids',true);
		$aids = explode(',',$aids);
		if(!empty($aids) && is_array($aids))
		{
			foreach($aids as $aid)
			{
				$aid = preg_replace("#[^0-9]#", "", $aid);
				
				//超级管理员不能删除
				if($aid=='1')
				{
					ShowMsg('管理员(admin)不能删除!', site_url("admin/system/account_index"),0,1000);
					exit;
				}
				
				$this->load->model('admin/User_model');
				$this->User_model->get_user_del($aid);
				 
		
				
			}
			ShowMsg('删除成功!', site_url("admin/system/account_index"),0,1000);
			exit;
		}
		else
		{
			ShowMsg('删除失败!', site_url("admin/system/account_index"),0,1000);
			exit;
		}
	}
	
	
	
	
	//登录日志
	public function login_log_index()
	{
		//检查是否有超级管理员权限
		$this->_check_admin();
		
		$whereSql = " WHERE 1=1 order by id desc";
		
		$query = "SELECT * FROM `crazy_system_log` $whereSql ";
		
		$list = $this->db->query($query);
		
		// 从数据库中获取列表总数
		$num = $list->num_rows();
		
		// 查询条件
		$search_str = "";
		
		// 分类数据初始化
		$params = array( 
			'total' => $num, 
			'listRows' => '10', 
			'pa' => $search_str 
		);
		
		// 分页类初始化
		$this->load->library('Page', $params);
		
		// 分页SQL语句
		$search_query = $query . "  limit {$this->page->limit} ";
		
		// 获取分页结果
		$reslut_list = $this->db->query($search_query);
		
		$data['list'] = $reslut_list;
		
		$data['page'] = $this->page->fpage();
		
		$this->load->view('admin/login_log_index', $data);
		
	
	}
	

    }
?>